package com.example.lab_3;

import java.util.Random;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;



public class RandomNumberThread extends Thread 
{
	
	public static final int STATE_STOP = 1;
    public static final int STATE_PAUSE = 2;
    public static final int STATE_RUNNING = 3;   
	private boolean mRun = false;
	private Intent random_number_intent;
	private int sleep_time;
	private int max_random_number;
	private int random_number;
	private int mMode;
	
	public RandomNumberThread(Intent random_number_intent,int sleep_time_in,int max_random_number){
		this.sleep_time = sleep_time_in;
		this.random_number_intent = random_number_intent;
		this.max_random_number = max_random_number;
	}

	@Override
	public void run() 
	{
		if(mRun) {
			printToScrinLog("the run is cald");
			this.startRandomNumber();
		}
	}
	
	public void setRunning(boolean b) {
        this.mRun = b;
    }
	
	public void setState(int mode) {
		this.mMode = mode;
    }
	
	public void doStart(){
		if( this.mMode != STATE_RUNNING){
			this.setState(STATE_RUNNING);
			this.setRunning(true);
		 }
	}
	 
	 public void doStop(){
		 if( this.mMode != STATE_STOP){
			 this.setState(STATE_STOP);
			 this.setRunning(false);
		 }
	 }
	 
	 public void doResume(){
		 if( this.mMode != STATE_RUNNING){
			 this.setState(STATE_STOP);
			 this.setRunning(true);
		 }
	 }
	 
	
    private void printToScrinLog(String logTex){
		System.out.println(logTex);
	}
    
    
    public void startRandomNumber(){
    	Random random = new Random();
    	
    	while(mRun){
      	  try {
              Thread.sleep(this.sleep_time);
          } catch (InterruptedException e) {
              Log.e("ERROR", "Thread Interrupted");
          }
      	  
      	  this.random_number = random.nextInt(100);
      	  this.setRandomNumberMessage(""+this.random_number);
    	}
    }
    
    public void setRandomNumberMessage(String random_number_tex){
    	
    	if(this.random_number_intent != null)
 	   	{
    		this.random_number_intent.putExtra(Lab_3.EXTRA_MESSAGE,random_number_tex);
/* 		   Message msg = this.random_number_intent.obtainMessage();
 		   Bundle b = new Bundle();
           b.putString("random_number_tex",random_number_tex);
           msg.setData(b);
           random_number_intent.sendMessage(msg);*/
 		
 	   	}else{
 		   System.out.println(random_number_tex);
 	   	}
    }
    
    public void setRandomNumberIntent(Intent random_number_intent){
    	this.random_number_intent = random_number_intent;
    }
    
	
}
